﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PureMVC.Patterns;
using PureMVC.Interfaces;
using GameEngine.Clients.Silverlight.View;
using MySettlers.Screens;
using GameEngine.Model.Data;
using MySettlers.View.Components.editor;
using GameEngine.Clients.Silverlight;
using GameEngine;
using SilverlightClientEditor;
using System.IO;
using System.Collections.Generic;
using GameEngine.View.Components;
using MySettlers.Game.View.Components;
using MySettlers.View;

namespace MySettlers.Commands
{
    public class Page_EditorEditGame_In_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            SilverlightEditorFacade.Instance.GetType();//Initialize editor facade

            var screen = new EditorEditGameUC();
            var mediator = MyViewFacade.Helper.CreateMenu<string, EditorEditGameUC>(
                MyFacade.EditorEditPageMediatorName, 
                screen, 
                MyFacade.PAGE_EDITOREDITGAME_OUT_COMMAND);

            var game = ModelFacade<int>.Instance.Game;
            
            var lvl = game.CurrentLevel;
            var worlds = lvl.Worlds.GetEnumerator();
            worlds.MoveNext();
            var world = worlds.Current.Value;
            world.Map.CameraPosition = new Point(Math.Round(world.Map.Size.X/2), Math.Round(world.Map.Size.Y/2));
            world.Map.Scale = 3f;
            //fazer detach das views dos worlds ao lvl
            var levelMed = MyViewFacade.Helper.GetLevelMediator(lvl.VO.Id);
            List<IWorldView<int, FrameworkElement>> views = new List<IWorldView<int, FrameworkElement>>();
            levelMed.ViewComponent.SetWorldViews(views);
            var mapMed = SilverlightEditorViewFacade.Helper.CreateMapPropertiesEditor(world.Map);
            var thingMed = SilverlightEditorViewFacade.Helper.CreateThingEditorMenu(null);
            var worldMed = MyViewFacade.Helper.GetWorldMediator(world.Id);
            worldMed.mapMediator.RegisterMapNavigator(screen.MapNavigator);
            worldMed.mapMediator.RegisterMapZoom(screen.MapZoom);
            worldMed.mapMediator.MouseOnItems(true);
            var worldEditorMed = SilverlightEditorViewFacade.Helper.CreateWorldPropertiesEditor(world);
            screen.SetWorldContainer(worldMed.ViewComponent as UserControl);
            screen.SetWorldEditorMenuContainer(worldEditorMed.ViewComponent as UserControl);
            screen.SetThingEditorMenuContainer(thingMed.ViewComponent as UserControl);
            screen.SetMapEditorMenuContainer(mapMed.ViewComponent as UserControl);
            //invisible ao tamnho do mapa

            var buildingMenuView = new BuildingMenuUC();
            var buildingMenuMediator = new BuildingMenuMediator(buildingMenuView, "");
            Facade.RegisterMediator(buildingMenuMediator);

            var peasantMenuView = new PeasantMenuUC();
            var peasantMenuMediator = new PeasantMenuMediator(peasantMenuView, "");
            Facade.RegisterMediator(peasantMenuMediator);


            MyFacadeHelper.DesktopNavigate(mediator.ViewComponent);
        }
    }

    public class Page_EditorEditGame_Out_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            var game = ModelFacade<int>.Instance.Game;
            var lvl = game.CurrentLevel;
            var worlds = lvl.Worlds.GetEnumerator();
            worlds.MoveNext();
            var world = worlds.Current.Value;

            switch (notification.Body as string)
            {
                case EditorEditGameUC.EXITBUTTON:
                    MyFacade.Instance.RemoveMediator(MyFacade.EditorEditPageMediatorName);
                    MyFacade.Instance.RemoveMediator(SilverlightEditorViewFacade.Helper.GetThingEditorMenuMediator().MediatorName);
                    MyFacade.Instance.RemoveMediator(SilverlightEditorViewFacade.Helper.GetMapEditorMenuMediator().MediatorName);
                    MyFacade.Instance.RemoveMediator(SilverlightEditorViewFacade.Helper.GetWorldEditorMenuMediator().MediatorName);
                    MyFacade.Instance.RemoveMediator(MyViewFacade.Helper.GetWorldMediator(world.Id).MediatorName);
                    SilverlightApplicationFacadeHelper.DestroyGame();
                    MyFacade.Instance.SendNotification(MyFacade.PAGE_EDITORMAINMENU_IN_COMMAND);
                    break;
                case EditorEditGameUC.SAVEBUTTON:
                    MyFacade.Instance.saveGame();
                    break;
            }
        }
    }
}
